<html>
    <head>
        <title>Itowns - Globe</title>

        <meta charset="UTF-8">
        <link rel="stylesheet" type="text/css" href="css/example.css">

        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="js/GUI/dat.gui/dat.gui.min.js"></script>
    </head>
    <body>
        <div id="viewerDiv"></div>
        <script src="js/GUI/GuiTools.js"></script>
        <script src="../dist/itowns.js"></script>
        <script src="js/loading_screen.js"></script>
        <script src="../dist/debug.js"></script>
        <script type="text/javascript">
            // Define initial camera position
            // Coordinate can be found on https://www.geoportail.gouv.fr/carte
            // setting is "coordonnée geographiques en degres decimaux"

            // Position near Annecy lake.
            // var positionOnGlobe = { longitude: 6.2230, latitude: 45.8532, altitude: 5000 };

            // Position near Gerbier mountain.
            var positionOnGlobe = { longitude: 4.22, latitude: 44.844, altitude: 4000 };

            // `viewerDiv` will contain iTowns' rendering area (`<canvas>`)
            var viewerDiv = document.getElementById('viewerDiv');

            // Instanciate iTowns GlobeView*
            var view = new itowns.GlobeView(viewerDiv, positionOnGlobe);

            var menuGlobe = new GuiTools('menuDiv', view);
            setupLoadingScreen(viewerDiv, view);

            // Add one imagery layer to the scene
            // This layer is defined in a json file but it could be defined as a plain js
            // object. See Layer* for more info.
            itowns.Fetcher.json('./layers/JSONLayers/Ortho.json').then(function _(config) {
                config.source = new itowns.WMTSSource(config.source);
                var layer = new itowns.ColorLayer('Ortho', config);
                view.addLayer(layer).then(menuGlobe.addLayerGUI.bind(menuGlobe));
            });
            // Add two elevation layers.
            // These will deform iTowns globe geometry to represent terrain elevation.
            function addElevationLayerFromConfig(config) {
                config.source = new itowns.WMTSSource(config.source);
                var layer = new itowns.ElevationLayer(config.id, config);
                view.addLayer(layer).then(menuGlobe.addLayerGUI.bind(menuGlobe));
            }
            itowns.Fetcher.json('./layers/JSONLayers/WORLD_DTM.json').then(addElevationLayerFromConfig);
            itowns.Fetcher.json('./layers/JSONLayers/IGN_MNT_HIGHRES.json').then(addElevationLayerFromConfig);

            function addMeshToScene() {
                // creation of the new mesh (a cylinder)
                var THREE = itowns.THREE;
                var geometry = new THREE.CylinderGeometry(0, 10, 60, 8);
                var material = new THREE.MeshBasicMaterial({ color: 0xff0000 });
                var mesh = new THREE.Mesh(geometry, material);

                // get the position on the globe, from the camera
                var cameraTargetPosition = view.controls.getLookAtCoordinate();

                // position of the mesh
                var meshCoord = cameraTargetPosition;
                meshCoord.setAltitude(cameraTargetPosition.altitude() + 30);

                // position and orientation of the mesh
                mesh.position.copy(meshCoord.as(view.referenceCrs).xyz());
                mesh.lookAt(new THREE.Vector3(0, 0, 0));
                mesh.rotateX(Math.PI / 2);

                // update coordinate of the mesh
                mesh.updateMatrixWorld();

                // add the mesh to the scene
                view.scene.add(mesh);

                // make the object usable from outside of the function
                view.mesh = mesh;
            }

            // Listen for globe full initialisation event
            view.addEventListener(itowns.GLOBE_VIEW_EVENTS.GLOBE_INITIALIZED, function globeInitialized() {
                // eslint-disable-next-line no-console
                console.info('Globe initialized');
                addMeshToScene();
                view.controls.setTilt(60, true);
            });
        </script>
    </body>
</html>
